/**
 * 989. 数组形式的整数加法
 */
export function addToArrayForm(num: number[], k: number): number[] {
  let ks = `${k}`.split('')
  let l1 = num.length - 1;
  let l2 = ks.length - 1;
  let list: number[] = []
  let carry = 0
  while (l1 >= 0 || l2 >= 0) {
    let x = l1 < 0 ? 0 : num[l1];
    let y = l2 < 0 ? 0 : Number(ks[l2]);
    let sum = x + y + carry;
    list.push(sum % 10)
    carry = Math.floor(sum / 10); // 余数取整；
    l1--;
    l2--;
  }
  if (carry) list.push(carry)
  return  list.reverse()
}
/*
自己写的 错误解法
function addToArrayForm(num: number[], k: number): number[] {
  // 取最短的长度
  let ks = `${k}`.split('')
  let lenB = num.length >= ks.length
  let len = lenB ? ks.length : num.length
  let resbefor = lenB ? num.slice(0, -len) : []
  let numArr = num.slice(-len)
  let kArr = ks.slice(-len)
  let res = []
  let ten = 0 // 是否满 10
  for (let i = len - 1; i >= 0; i--) {
    const sum = Number(numArr[i]) + Number(kArr[i]) + ten
    if(i > 0){
      if (sum < 10) {
        res[i] = sum
        ten = 0
      } else{
        ten = 1
        res[i] = sum % 10
      }
    }
    // 处理最后一个
    if(i===0){
      if(sum<10){
        res[i] = sum
        ten = 0
      }else {
        res[i] = 0
        return resbefor.concat(res)
      }
    }
  }

  return resbefor.concat(res)
};
console.log(addToArrayForm([9,9,9,9,9,9,9,9,9,9], 1));
*/
